@extends('layouts.admin')

@section('content')
<link rel="stylesheet" href="{{ asset('css/admin/bootstrap-datetimepicker.css') }}">
<link rel="stylesheet" href="{{ asset('css/admin/bootstrap-dialog.min.css') }}">
<link rel="stylesheet" href="{{ asset('kindeditor/themes/default/default.css') }}">
<link rel="stylesheet" href="{{ asset('kindeditor/plugins/code/prettify.css') }}">

<script src=" {{ asset('kindeditor/kindeditor-all.js') }}"></script>
<script src=" {{ asset('kindeditor/plugins/code/prettify.js') }}"></script>
<script src=" {{ asset('kindeditor/lang/zh-CN.js') }}"></script>
<script src=" {{ asset('js/admin/moment-with-locales.min.js') }}"></script>
<script src=" {{ asset('js/admin/bootstrap-datetimepicker.js') }}"></script>

<style>
.breadcrumb {
  line-height: 15px;
  margin-bottom: 0px;
}
</style>

<?php $pagename = '文章添加' ?>

<div class="admin-home">
  <br>
  <div class="panel panel-info">
    <div class="panel-heading">
      <h1 class="panel-title">
        <ol class="breadcrumb">
          <li><a href="{{ url('admin/article-list') }}">后台首页</a></li>
          <li><a href="{{ url('admin/article-list') }}">文章列表</a></li>
          <li class="active">添加文章</li>
        </ol>
      </h1>
    </div>
  </div>
  
  <div class="panel-body">
    <div class="row" style="margin: 10px 0;">
      <form class="form-horizontal" role="form" onsubmit="submitTheArticle(); return false;">
        <div class="form-group">
          <label for="title" class="col-xs-2 control-label">标题</label>
          <div class="col-xs-10">
            <input type="text" class="form-control" id="title" placeholder="标题" value="{{ $article ? $article->title : '' }}">
          </div>
        </div>

        <div class="form-group">
          <label for="author" class="col-xs-2 control-label">作者</label>
          <div class="col-xs-10">
            <input type="text" class="form-control" id="author" placeholder="作者，非必填" value="{{ $article ? $article->author : '' }}">
          </div>
        </div>

        <div class="form-group">
          <label for="enable" class="col-xs-2 control-label">文章类别</label>
          <div class="col-xs-8">
            <select class="form-control" id="type">
              @foreach (['公告','法律'] as $type)
                <option value="{{ $type }}"
                @if ( !empty($article) && $article->type == $type  )
                  selected 
                @endif
                >{{ $type }}</option>
              @endforeach
            </select>
          </div>
          <label for="enable" class="col-xs-2 control-label" style="text-align: left;color:#af1010; padding-left: 0;">文章会按类别显示</label>
        </div>

        <div class="form-group">
          <label for="enable" class="col-xs-2 control-label">是否开启</label>
          <div class="col-xs-8">
            <select class="form-control" id="enable">
              @foreach ([1=>'开启',0=>'关闭'] as $key => $enable)
                <option value="{{ $key }}"
                @if ( !empty($article) && $article->enable == $enable  )
                  selected 
                @endif
                >{{ $enable }}</option>
              @endforeach
            </select>
          </div>
          <label for="enable" class="col-xs-2 control-label" style="text-align: left;color:#af1010; padding-left: 0;">开启后才可查看</label>
        </div>

        <div class="form-group">
          <label for="publish-time" class="col-xs-2 control-label">发布时间</label>
          <div class="col-xs-4">
            <div class='input-group date' id='datetimepicker'>
              <input type='text' id="publish-time" class="form-control" value="{{ $article ? $article->created_at : date('Y-m-d H:i:s') }}">
              <span class="input-group-addon">
                <span class="glyphicon glyphicon-time"></span>
              </span>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label for="describe" class="col-xs-2 control-label">简述</label>
          <div class="col-sm-10">
            <textarea name="describe" id="describe" rows="4" style="width: 100%;">{{ $article ? $article->describe : '' }}</textarea>
          </div>
        </div>
        
        <div class="form-group">
          <label for="content" class="col-xs-2 control-label">文章</label>
          <div class="col-sm-10">
            <textarea name="content" id="content" style="width:100%;min-height:500px;visibility:hidden;">{!! $article ? $article->content : '' !!}</textarea>
          </div>
        </div>
        <div class="form-group">
          <button type="submit" class="col-xs-12 btn btn-success">
            @if ( empty($article) )
              修改
            @else
              添加
            @endif
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<script>
KindEditor.ready(function(K) {
  editor = K.create('#content', {
    cssPath : '/kindeditor/plugins/code/prettify.css',
    filterMode : true,
    uploadJson : '/admin/article-upload-img',
  });
  prettyPrint();
});
moment.locale('zh-cn');
$(function () {
  $('#datetimepicker').datetimepicker({
    defaultDate: moment().format('YYYY-MM-DD HH:mm:ss'),
    locale: 'zh-cn',
    format: 'YYYY-MM-DD HH:mm:ss'
  });
});

var submitTheArticle = function() {
  
  var content = editor.html();
  var title   = $("#title").val();
  var enable   = $("#enable").val();
  var type   = $("#type").val();
  var publish_time   = $("#publish-time").val();
  var describe   = $("#describe").val();
  var author   = $("#author").val();

  if( title.length == 0 )
  {
    $("#title").css('border-color','#FF0000');
    return false;
  }


  var data = {
    content:content,
    title: title,
    author:author,
    enable:enable,
    type:type,
    publish_time:publish_time,
    describe:describe,
    _token: '{{ csrf_token() }}'
  }
  var url = ' {{ url('admin/article-save') }}';
  var msg = '添加';

  @if ( ! empty($article) )
    url = '{{ url('admin/article-update/'.$article->id) }}';
    msg = '编辑';
  @endif

  $.ajax({
    url: url,
    method: 'POST',
    data: data,
    dataType: 'json',
    success: function(data) {
      swal( msg+"成功！", "1 秒之后页面将自动跳转", "success")
      window.setTimeout(function() {
        location.href = '/admin/article-show/' + data.id;
      }, 1000);
    },
    error: function() {
      swal("提交出错，请刷新o(╯□╰)o", "", "error")
    }
  });
}
</script>

@stop